import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_common/global_getx.dart';
import 'package:basic_widgets/bg_widget.dart';
import 'package:basic_widgets/button_widget.dart';
import 'package:basic_widgets/image_widget.dart';
import 'package:basic_widgets/select_widget.dart';
import 'package:flutter/gestures.dart';
import 'home_controller.dart';

class HomePage extends GetView<HomeController> {
  final controler =Get.put<HomeController>(HomeController());
final globalGetx=Get.put<GlobalGetx>(GlobalGetx());
  @override
  Widget build(BuildContext context) {
    ScreenHelper.init(context);
    return Scaffold(
      backgroundColor: AppTheme.appBgColor,
      appBar: AppBar(
          backgroundColor: AppTheme.appBgColor,
          elevation: 0,
          automaticallyImplyLeading: false,
          title: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              SelectWidget(
                package: "basic_common",
                items: [
                  MenuItem(
                      label: "在线",
                      value: 0,
                      color: AppTheme.primyColor,
                      icon: 'assets/home/icon_online.svg'),
                  MenuItem(
                      label: "工作中",
                      value: 1,
                      color: AppTheme.redtextColor,
                      icon: 'assets/home/icon_manglu.svg'),
                  MenuItem(
                      label: "忙碌",
                      value: 2,
                      color: AppTheme.dartWeakTextColor,
                      icon: 'assets/home/icon_xiuxi.svg'),
                  MenuItem(
                      label: "停诊",
                      value: 4,
                      color: AppTheme.redtextColor,
                      icon: 'assets/home/icon_tingzhen.svg'),
                ],
                value: controller.stateStatus.value,
                valueChanged: (menuItem) {
                  controller.onlineState.value = menuItem.label;
                  controller.stateIcon.value = menuItem.icon;
                  controller.stateColor.value = menuItem.color;
                  controller.stateStatus.value = menuItem.value;
                  controller.updateState();
                },
                clickChild: Stack(
                  children: [
                    circleImg(
                        head: globalGetx.doctorInfo != null &&
                                globalGetx.doctorInfo!.profilePhoto != null
                            ? globalGetx.doctorInfo!.profilePhoto!
                            : ''),
                    Positioned(
                      bottom: 0,
                      right: 0,
                      child: Obx(() => svgImage(
                          assets: controller.stateIcon.value,
                          width: 16,
                          package: "basic_common")),
                    )
                  ],
                ),
              ),
              Padding(
                padding:
                    EdgeInsets.symmetric(horizontal: ScreenHelper.width(6)),
                child: GetBuilder<GlobalGetx>(
                  id: 'doctorInfo',
                  builder: (controller) => Text(
                    (globalGetx.doctorInfo == null ||
                            globalGetx.doctorInfo!.doctorNick!.isEmpty)
                        ? ''
                        : globalGetx.doctorInfo!.doctorNick!,
                    style: TextStyle(
                        color: AppTheme.dartTextColor,
                        fontSize: ScreenHelper.sp(16)),
                  ),
                ),
              ),
              Obx(() => Text(
                    controller.onlineState.value ?? '',
                    style: TextStyle(
                        color: controller.stateColor.value,
                        fontSize: ScreenHelper.sp(13)),
                  )),
            ],
          ),
          centerTitle: false,
          flexibleSpace: Container(
            decoration: const BoxDecoration(
              gradient: LinearGradient(colors: [
                Color(0xFFEAFFF8),
                Color(0xFFF7F7F7),
              ], begin: Alignment.topCenter, end: Alignment.bottomCenter),
            ),
          )),
      body: SingleChildScrollView(
        physics: const ClampingScrollPhysics(),
        primary: false,
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              /// 顶部内容
              Stack(
                children: [
                  GetBuilder<HomeController>(
                    id: 'statis',
                    builder: (controller) => gradientBgContainer(
                        child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: controller.titles
                                .map<Widget>((item) => buildTopItem(
                                    content: item['content']!,
                                    title: item['title']!))
                                .toList())),
                  ),
                  Positioned(
                    right: 10,
                    top: 8,
                    child: svgImage(
                        assets: "assets/home/add_red.svg",
                        width: 20,
                        alignment: Alignment.topRight,
                        colorFilter: const ColorFilter.mode(
                            Color.fromRGBO(255, 255, 255, .33),
                            BlendMode.srcIn),package: "basic_common"),
                  )
                ],
              ),

              /// 常用功能
              Container(
                padding:
                    EdgeInsets.symmetric(vertical: ScreenHelper.height(10)),
                decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                child: GridView.builder(
                  physics: const NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 4,
                    crossAxisSpacing: 10,
                  ),
                  itemBuilder: (context, index) {
                    final itemData = controller.commonUseList[index];
                    return GestureDetector(
                      onTap: () => Get.toNamed(itemData['page']!,
                          arguments: {"title": itemData['title']!}),
                      child: buildTopImgItem(
                          icon: itemData['icon']!,
                          title: itemData['title']!,
                          packageName: "basic_common"),
                    );
                  },
                  itemCount: controller.commonUseList.length,
                ),
              ),

              buildTodoListItem(),
              SizedBox(
                height: ScreenHelper.height(320),
                child: TabBarView(
                    controller: controller.tabController,
                    children: controller.todoTitleList
                        .map<Widget>((e) => buildTodo())
                        .toList()),
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget buildTodo() {
    return GetBuilder<HomeController>(
      id: 'todoList',
      builder: (controller) => GridView.builder(
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            childAspectRatio: 1.7,
            crossAxisCount: 2,
            crossAxisSpacing: 10,
            mainAxisSpacing: 2),
        itemCount: controller.todoListContent.length,
        itemBuilder: (context, index) {
          var item = controller.todoListContent[index];
          return GestureDetector(
            onTap: () => Get.toNamed(item['page']!,
                arguments: {"title": item['title']!}),
            child: buildTodoItem(
              icon: item['icon']!,
              num: item['num']!,
              title: item['title']!,
              color: item['color']!,
            ),
          );
        },
      ),
    );
  }

  /// 顶部内容Item项
  /// title 标题名称
  /// content 内容数据
  Widget buildTopItem({required String title, required String content}) {
    TextStyle titleStrle = TextStyle(
      fontSize: ScreenHelper.sp(13),
      color: Colors.white,
    );
    TextStyle contentStyle = TextStyle(
        fontSize: ScreenHelper.sp(18),
        color: Colors.white,
        fontWeight: FontWeight.bold);
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: titleStrle,
          textAlign: TextAlign.start,
        ),
        const SizedBox(height: 10),
        Text(
          content,
          style: contentStyle,
          textAlign: TextAlign.start,
        )
      ],
    );
  }

  /// 待办事项 tabBar Title
  Widget buildTodoListItem() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Expanded(
          child: SizedBox(
            height: 48,
            child: TabBar(
                controller: controller.tabController,
                indicatorSize: TabBarIndicatorSize.label,
                labelPadding: EdgeInsets.zero,
                // indicatorPadding: EdgeInsets.only(
                //     right: ScreenHelper.width(70),
                //     left: ScreenHelper.width(10)),
                padding: EdgeInsets.zero,
                indicatorWeight: 1,
                dragStartBehavior: DragStartBehavior.down,
                indicatorColor: AppTheme.primyColor,
                labelColor: AppTheme.dartTextColor,
                labelStyle: TextStyle(
                    color: AppTheme.dartTextColor,
                    fontSize: ScreenHelper.sp(16)),
                unselectedLabelColor: AppTheme.weakTextColor,
                tabs: controller.todoTitleList
                    .map<Widget>((item) => buildTitleListWidget(title: item))
                    .toList()),
          ),
        ),
      ],
    );
  }

  /// 待办事项标题Item
  buildTitleListWidget({required String title}) {
    return Align(
      alignment: Alignment.center,
      child: Tab(
        text: title,
      ),
    );
  }

  /// 待办事项 Item页
  /// icon 图标地址
  /// title 标题
  /// num 数量
  Widget buildTodoItem(
      {required String icon,
      required String title,
      required String num,
      required String color}) {
    TextStyle titleStrle = TextStyle(
        fontSize: ScreenHelper.sp(15),
        color: AppTheme.dartTextColor,
        fontWeight: FontWeight.w500);
    TextStyle contentStyle = TextStyle(
        fontSize: ScreenHelper.sp(20),
        color: AppTheme.dartTextColor,
        fontWeight: FontWeight.w500);
    return Container(
        margin: const EdgeInsets.only(top: 10),
        padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
        decoration: BoxDecoration(
            color: Color(int.parse(color)),
            borderRadius: BorderRadius.circular(4)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.min,
          children: [
            Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: titleStrle,
                  textAlign: TextAlign.start,
                ),
                const SizedBox(height: 10),
                Text(
                  num,
                  style: contentStyle,
                  textAlign: TextAlign.start,
                )
              ],
            ),
            Image.asset(
              icon,
              width: ScreenHelper.width(50),
              height: ScreenHelper.width(50),
              package: "basic_common",
              fit: BoxFit.fill,
            )
          ],
        ));
  }
}
